package com.ruoyi.mx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.WxLoginService;
import com.ruoyi.framework.web.service.WxUser;
import com.ruoyi.mx.domain.MxActivity;
import com.ruoyi.mx.domain.MxComment;
import com.ruoyi.mx.domain.MxGoodsSpu;
import com.ruoyi.mx.domain.MxUser;
import com.ruoyi.mx.service.IMxActivityService;
import com.ruoyi.mx.service.IMxCommentService;
import com.ruoyi.mx.service.IMxGoodsSpuService;
import com.ruoyi.mx.service.IMxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/mx/check")
@Api(tags = "前端控制器")
public class CheckController {

    @Autowired
    private WxLoginService wxLoginService;
    @Autowired
    private IMxUserService iMxUserService;
    @Autowired
    private IMxActivityService iMxActivityService;
    @Autowired
    private IMxCommentService iMxCommentService;
    @Autowired
    private IMxGoodsSpuService iMxGoodsSpuService;

    @ApiOperation(value = "写评论")
    @PostMapping("/writeComment")
    //HttpServletRequest 对请求进行筛选
    public R writeComment(@RequestBody MxComment mxComment, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        //&&并且
        if (StringUtils.isNotNull(mxUser) && StringUtils.isNotNull(mxComment)) {
            //通过评论实体的活动id查询
            MxActivity mxActivity = iMxActivityService.getById(mxComment.getAid());
            mxActivity.setComments(mxActivity.getComments() + 1);
            iMxActivityService.updateById(mxActivity);
            //代表子评论
            if (!"0".equals(mxComment.getPid())) {
                //查找父评论
                MxComment pComment = iMxCommentService.getById(mxComment.getPid());
                pComment.setReplies(pComment.getReplies() + 1);
                iMxCommentService.updateById(pComment);
            }
            mxComment.setUid(mxUser.getId());
            iMxCommentService.save(mxComment);
            return R.success("评论成功");
        }
        return R.error("评论出错");
    }

    @ApiOperation(value = "收藏活动")
    @GetMapping("/activityCollect/{aid}")
    //HttpServletRequest 对请求进行筛选
    public R activityCollect(@PathVariable String aid, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        if (StringUtils.isNotNull(mxUser)) {
            MxActivity activity = iMxActivityService.getById(aid);
            if(StringUtils.isNotNull(activity)){
                Integer count=iMxActivityService.countCollections(aid,mxUser.getId());
                if(count==0){
                    activity.setCollections(activity.getCollections()+1);
                    iMxActivityService.updateById(activity);
                    iMxActivityService.insertCollections(aid,mxUser.getId());
                    return R.success("收藏成功");
                }
            }
        }
       return R.error("收藏出错");
    }

    @ApiOperation(value = "取消收藏")
    @GetMapping("/activityCollectCancel/{aid}")
    //HttpServletRequest 对请求进行筛选
    public R activityCollectCancel(@PathVariable String aid, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        if (StringUtils.isNotNull(mxUser)) {
            MxActivity activity = iMxActivityService.getById(aid);
            if(StringUtils.isNotNull(activity)){
                activity.setCollections(activity.getCollections()-1);
                iMxActivityService.updateById(activity);
                Integer delNum=iMxActivityService.delCollections(aid,mxUser.getId());
                if(delNum==1){
                    return R.success("取消收藏");
                }
            }
        }
        return R.error("取消收藏出错");
    }


    @ApiOperation(value = "点赞活动")
    @GetMapping("/activityZan/{aid}")
    //HttpServletRequest 对请求进行筛选
    public R activityZan(@PathVariable String aid, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        if (StringUtils.isNotNull(mxUser)) {
            MxActivity activity = iMxActivityService.getById(aid);
            if(StringUtils.isNotNull(activity)){
                Integer count=iMxActivityService.countActivityZan(aid,mxUser.getId());
                if(count==0){
                    //删除点踩记录，返回删除数量
                    Integer cai=iMxActivityService.delCai(aid,mxUser.getId());
                    if(cai==1){
                        activity.setUnlikes(activity.getUnlikes()-1);
                    }
                    activity.setLikes(activity.getLikes()+1);
                    iMxActivityService.updateById(activity);
                    iMxActivityService.insertAZan(aid,mxUser.getId());
                    return R.success("点赞成功");
                }
            }
        }
        return R.error("点赞出错");
    }

    @ApiOperation(value = "取消点赞")
    @GetMapping("/activityZanCancel/{aid}")
    //HttpServletRequest 对请求进行筛选
    public R activityZanCancel(@PathVariable String aid, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        if (StringUtils.isNotNull(mxUser)) {
            MxActivity activity = iMxActivityService.getById(aid);
            if(StringUtils.isNotNull(activity)){
                Integer zan=iMxActivityService.delZan(aid,mxUser.getId());
                if(zan==1){
                    activity.setLikes(activity.getLikes()-1);
                    iMxActivityService.updateById(activity);
                    return R.success("取消点赞");
                }
            }
        }
        return R.error("取消点赞出错");
    }

    @ApiOperation(value = "点踩活动")
    @GetMapping("/activityCai/{aid}")
    //HttpServletRequest 对请求进行筛选
    public R activityCai(@PathVariable String aid, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        if (StringUtils.isNotNull(mxUser)) {
            MxActivity activity = iMxActivityService.getById(aid);
            if(StringUtils.isNotNull(activity)){
                Integer count=iMxActivityService.countActivityCai(aid,mxUser.getId());
                if(count==0){
                    //删除点踩记录，返回删除数量
                    Integer zan=iMxActivityService.delZan(aid,mxUser.getId());
                    if(zan==1){
                        activity.setLikes(activity.getLikes()-1);
                    }
                    activity.setUnlikes(activity.getUnlikes()+1);
                    iMxActivityService.updateById(activity);
                    iMxActivityService.insertACai(aid,mxUser.getId());
                    return R.success("点踩成功");
                }
            }
        }
        return R.error("点踩出错");
    }

    @ApiOperation(value = "取消点踩")
    @GetMapping("/activityCaiCancel/{aid}")
    //HttpServletRequest 对请求进行筛选
    public R activityCaiCancel(@PathVariable String aid, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        if (StringUtils.isNotNull(mxUser)) {
            MxActivity activity = iMxActivityService.getById(aid);
            if(StringUtils.isNotNull(activity)){
                Integer cai=iMxActivityService.delCai(aid,mxUser.getId());
                if(cai==1){
                    activity.setUnlikes(activity.getUnlikes()-1);
                    iMxActivityService.updateById(activity);
                    return R.success("取消点踩");
                }
            }
        }
        return R.error("取消点踩出错");
    }

    @ApiOperation(value = "点赞评论")
    @GetMapping("/commentZan/{cid}")
    //HttpServletRequest 对请求进行筛选
    public R commentZan(@PathVariable String cid, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        if (StringUtils.isNotNull(mxUser)) {
            MxComment comment = iMxCommentService.getById(cid);
            if(StringUtils.isNotNull(comment)){
                Integer count=iMxCommentService.countCZan(cid,mxUser.getId());
                if(count==0){
                    //删除点踩记录，返回删除数量
                    Integer cai=iMxCommentService.delCCai(cid,mxUser.getId());
                    if(cai==1){
                        comment.setUnlikes(comment.getUnlikes()-1);
                    }
                    comment.setLikes(comment.getLikes()+1);
                    iMxCommentService.updateById(comment);
                    iMxCommentService.insertCZan(cid,mxUser.getId());
                    return R.success("点赞成功");
                }
            }
        }
        return R.error("点赞出错");
    }

    @ApiOperation(value = "取消点赞评论")
    @GetMapping("/commentZanCancel/{cid}")
    //HttpServletRequest 对请求进行筛选
    public R commentZanCancel(@PathVariable String cid, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        if (StringUtils.isNotNull(mxUser)) {
            MxComment comment = iMxCommentService.getById(cid);
            if(StringUtils.isNotNull(comment)){
                Integer zan=iMxCommentService.delCZan(cid,mxUser.getId());
                if(zan==1){
                    comment.setLikes(comment.getLikes()-1);
                    iMxCommentService.updateById(comment);
                    return R.success("取消点赞");
                }
            }
        }
        return R.error("取消点赞出错");
    }

    @ApiOperation(value = "点踩评论")
    @GetMapping("/commentCai/{cid}")
    //HttpServletRequest 对请求进行筛选
    public R commentCai(@PathVariable String cid, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        if (StringUtils.isNotNull(mxUser)) {
            MxComment comment = iMxCommentService.getById(cid);
            if(StringUtils.isNotNull(comment)){
                Integer count=iMxCommentService.countCCai(cid,mxUser.getId());
                if(count==0){
                    //删除点踩记录，返回删除数量
                    Integer zan=iMxCommentService.delCZan(cid,mxUser.getId());
                    if(zan==1){
                        comment.setLikes(comment.getLikes()-1);
                    }
                    comment.setUnlikes(comment.getUnlikes()+1);
                    iMxCommentService.updateById(comment);
                    iMxCommentService.insertCCai(cid,mxUser.getId());
                    return R.success("点踩成功");
                }
            }
        }
        return R.error("点踩出错");
    }

    @ApiOperation(value = "取消点踩评论")
    @GetMapping("/commentCaiCancel/{cid}")
    //HttpServletRequest 对请求进行筛选
    public R commentCaiCancel(@PathVariable String cid, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        if (StringUtils.isNotNull(mxUser)) {
            MxComment comment = iMxCommentService.getById(cid);
            if(StringUtils.isNotNull(comment)){
                Integer cai=iMxCommentService.delCCai(cid,mxUser.getId());
                if(cai==1){
                    comment.setUnlikes(comment.getUnlikes()-1);
                    iMxCommentService.updateById(comment);
                    return R.success("取消点踩");
                }
            }
        }
        return R.error("取消点踩出错");
    }

    @ApiOperation(value = "获取活动当前用户状态")
    @GetMapping("/getActivityUserStatus/{aid}")
    public R getActivityUserStatus(@PathVariable String aid,HttpServletRequest request){
        MxUser user = getMxUserByRequest(request);
        String uid = user.getId();
        if(StringUtils.isNotNull(user)){
            MxActivity activity = iMxActivityService.getById(aid);
            if(StringUtils.isNotNull(activity)){
                //判断用户是否收藏该活动
                Integer collections=iMxActivityService.countCollections(aid,uid);
                //判断用户是否赞了该活动
                Integer aZan=iMxActivityService.countActivityZan(aid,uid);
                //判断用户是否踩了该活动  大意了 这里
                Integer aCai=iMxActivityService.countActivityCai(aid,uid);
                QueryWrapper<MxComment> wrapper=new QueryWrapper<>();
                wrapper.eq("aid",aid)
                        .orderByDesc("id");
                List<MxComment> commentList=iMxCommentService.list(wrapper);
                //存放该活动id下的评论集合  赞和踩的数量
                // ArrayList<HashMap<String,Object>> cZanAndCai=new ArrayList<>();
                ArrayList<Integer> cZan=new ArrayList<>();
                ArrayList<Integer> cCai=new ArrayList<>();
                for (MxComment mxComment : commentList) {
                    //HashMap<String,Object> cMap=new HashMap<>();
                    String cid=mxComment.getId();
                    Integer cZanNumber = iMxCommentService.countCZan(cid, uid);
                    Integer cCaiNumber = iMxCommentService.countCCai(cid, uid);
                    cZan.add(cZanNumber);
                    cCai.add(cCaiNumber);
                }
                HashMap<String,Object> map=new HashMap<>();
                map.put("aCollect",collections);
                map.put("aZan",aZan);
                map.put("aCai",aCai);
                map.put("cZan",cZan);
                map.put("cCai",cCai);
                return R.success("获取成功",map);
            }
        }
        return R.error("获取失败");
    }

    @ApiOperation(value ="获取商品当前用户状态" )
    @GetMapping("/getGoodsUserStatus/{gid}")
    public R getGoodsUserStatus(@PathVariable String gid,HttpServletRequest request){
        MxUser user = getMxUserByRequest(request);
        String uid = user.getId();
        if(StringUtils.isNotNull(user)){
            MxGoodsSpu goods =iMxGoodsSpuService.getById(gid);
            if(StringUtils.isNotNull(goods)) {
                //判断用户是否收藏该商品
                Integer goodsCount=iMxGoodsSpuService.countGoodsCollections(gid,uid);
                return R.success("获取成功",goodsCount);
            }
        }
       return R.error("获取失败");
    }

    @ApiOperation(value = "收藏商品")
    @GetMapping("/goodsCollect/{gid}")
    //HttpServletRequest 对请求进行筛选
    public R goodsCollect(@PathVariable String gid, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        if (StringUtils.isNotNull(mxUser)) {
            MxGoodsSpu goods = iMxGoodsSpuService.getById(gid);
            String uid=mxUser.getId();
            if(StringUtils.isNotNull(goods)){
                Integer count=iMxGoodsSpuService.countGoodsCollections(gid,uid);
                if(count==0){
                    //设置该商品收藏数+1
                    goods.setStock(goods.getStock()+1);
                    iMxGoodsSpuService.updateById(goods);
                    //插入用户收藏商品记录
                    iMxGoodsSpuService.insertGoodsCollections(gid,uid);
                    return R.success("收藏商品成功");
                }
            }
        }
        return R.error("收藏出错");
    }

    @ApiOperation(value = "取消收藏商品")
    @GetMapping("/goodsCollectCancel/{gid}")
    //HttpServletRequest 对请求进行筛选
    public R goodsCollectCancel(@PathVariable String gid, HttpServletRequest request) {
        MxUser mxUser = getMxUserByRequest(request);
        String uid=mxUser.getId();
        if (StringUtils.isNotNull(mxUser)) {
            MxGoodsSpu goods = iMxGoodsSpuService.getById(gid);
            if(StringUtils.isNotNull(goods)){
                //activity.setCollections(activity.getCollections()-1);
                goods.setStock(goods.getStock()-1);
                iMxGoodsSpuService.updateById(goods);
                Integer delNum=iMxGoodsSpuService.delGoodsCollections(gid,uid);
                if(delNum==1){
                    return R.success("取消收藏成功");
                }
            }
        }
        return R.error("取消收藏出错");
    }


    private MxUser getMxUserByRequest(HttpServletRequest request) {
        String token = wxLoginService.getToken(request);
        WxUser wxUser = wxLoginService.getWxUser(token);
        String openid = wxUser.getOpenid();
        QueryWrapper<MxUser> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        MxUser mxUser = iMxUserService.getOne(wrapper);
        return mxUser;
    }
}

